/**
 * Created by John on 2016/7/1.
 */
var demo = new Sample();

// demo.async();
// demo.parallel();
// demo.asyncEvent();

function Sample() {
    this.async = function () {
        var fs = require('fs');

        fs.readFile('/path', function (err, file) {
            console.log('读取文件完成')
        });
        console.log('发起读取文件');
    };

    this.parallel = function () {
        var fs = require('fs');

        function done1(err, file) {
            console.log('读取文件1完成');
        }

        function done2(err, file) {
            console.log('读取文件2完成');
        }

        fs.readFile('/path1', done1);
        fs.readFile('/path2', done2);
    };

    this.asyncEvent = function () {
        function app(req, res) {
            var postData = '';
            req.setEncoding('utf-8');

            function trunk(data) {
                postData += data;
            }

            function done() {
                res.end(postData);
            }

            req.on('data', trunk);
            req.on('end', done);
        }

        var http = require('http');
        http.createServer(app).listen(8080);
        console.log('服务器启动完成');
    }
}


